package com.keste.ojt1.view.task10.kvaka.bean;

import javax.faces.event.ValueChangeEvent;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

import javax.el.ExpressionFactory;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import org.apache.myfaces.trinidad.model.UploadedFile;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

public class UploadFileBean {
    public UploadFileBean() {
    }

  public void setUploadfile(RichInputFile uploadfile) {
      this.uploadFile = uploadfile;
  }

  public RichInputFile getUploadfile() {
      return uploadFile;
  }

  public void fileprocessor(ValueChangeEvent valueChangeEvent) {
  }
  private RichInputFile uploadFile;
  private BindingContainer bindings = null;

  /**
   * This method returns the instance of BindingContainer.
   * @return BindingContainer.
   */
  private BindingContainer getBindings() {

      if (this.bindings == null) {
          this.bindings = (BindingContainer)getDCBindingContainer();
      }
      return bindings;
  }

  /**
   * generic method to get handle to DCBindingContainer.
   * @return DCBindingContainer handle
   */
  public DCBindingContainer getDCBindingContainer() {

      FacesContext fctx = FacesContext.getCurrentInstance();
      ExpressionFactory expFactory =
          FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
      DCBindingContainer bc =

          (DCBindingContainer)expFactory.createValueExpression(fctx.getELContext(),
                                                               "#{bindings}",
                                                               DCBindingContainer.class).getValue(fctx.getELContext());

      return bc;
  }


  public void setUploadFile(RichInputFile uploadFile) {
      this.uploadFile = uploadFile;
  }

  public RichInputFile getUploadFile() {
      return uploadFile;
  }

    public void accessDateFromFile(ValueChangeEvent valueChangeEvent) {
      UploadedFile upFile = (UploadedFile)valueChangeEvent.getNewValue();

      List list = new ArrayList();

      try {
          BufferedReader reader =
              new BufferedReader(new InputStreamReader(upFile.getInputStream()));

          for (int i = 0; i < 7; i++) {
              String line = reader.readLine();
              StringTokenizer stok = new StringTokenizer(line, ",");
              List list1 = new ArrayList();
              while (stok.hasMoreTokens()) {

                  list1.add(stok.nextToken());
              }
              list.add(list1);
          }
          System.out.println("Token2: " + list);
          BindingContainer bindings = getBindings();
          OperationBinding op = bindings.getOperationBinding("setCSVDatatoVO");
          op.getParamsMap().put("list", list);
          if (op != null) {
              op.execute();
          }
      } catch (Exception e) {
          System.out.println("Trace: " + e);
      }
    }
}
